var menus = [
  {
    name: "主页",
    url: "./",
    icon: "icon--tabler--package",
    active: true,
  },
  {
    name: "工具集",
    url: "",
    icon: "icon--tabler--tools",
    children: [
      {
        name: "Bookmarks",
        url: "https://tools.94bug.top/vue3/form-sites.html",
        icon: "icon--tabler--bookmark-edit",
      },
      {
        name: "去水印",
        url: "tools/xhs.html",
        icon: "icon--tabler--star",
      },
      {
        name: "百家姓",
        url: "tools/bjx.html",
        icon: "icon--tabler--users-group",
      },
    ],
  },
];

$(document).ready(function () {
  var curPathName = window.location.pathname;
  var absoultePath = "./";
  if (window.location.host == "tabler.94bug.top") {
    absoultePath += "../";
  } else {
    var pathLevel = curPathName.split("/").length - 2;
    if (pathLevel > 1) {
      for (var i = 0; i < pathLevel - 1; i++) {
        absoultePath += "../";
      }
    }
  }

  // 加载外部模板文件
  $.get(`${absoultePath}templates/header.html`, function (templateString) {
    for (var i = 0; i < menus.length; i++) {
      var menu = menus[i];
      if (menu.url != "" && curPathName.endsWith(menu.url)) {
        menu.active = true;
      } else {
        menu.active = false;
        if (menu.children) {
          for (var j = 0; j < menu.children.length; j++) {
            var child = menu.children[j];
            if (curPathName.endsWith(child.url)) {
              menu.active = true;
              child.active = true;
            } else {
              child.active = false;
            }
            child.url =
              (child.url.startsWith("http") ? "" : absoultePath) + child.url;
          }
        }
      }
      menu.url = (menu.url.startsWith("http") ? "" : absoultePath) + menu.url;
    }

    // 数据对象
    var data = {
      pathName: curPathName,
      absoultePath: absoultePath,
      menus: menus,
    };
    // console.log('menus:', data)

    // 渲染模板
    var renderedHtml = template.render(templateString, data);

    // 将渲染后的 HTML 添加到页面
    $("#header").html(renderedHtml);
  });

  //$("#footer").load(`${absoultePath}templates/footer.html`);
});

function maskShow(content) {
  //console.log(content);
  document.getElementById("maskTitle").innerHTML = content;
  document.getElementById("mask").classList.add("d-inline");
}

function maskHide() {
  document.getElementById("maskTitle").innerHTML = "正在加载中";
  document.getElementById("mask").classList.remove("d-inline");
}

function detectDeviceAndOS() {
  const userAgent = navigator.userAgent;

  // 判断设备类型
  let deviceType;
  if (/Mobi|Android/i.test(userAgent)) {
    deviceType = "手机";
  } else {
    deviceType = "PC";
  }

  // 判断操作系统
  let osType;
  if (/iPhone|iPad|iPod/i.test(userAgent)) {
    osType = "iOS";
  } else if (/Android/i.test(userAgent)) {
    osType = "Android";
  } else {
    osType = "其他";
  }

  return { deviceType, osType };
}
